package com.stone.framework.task.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.task.JobLanuchReqBody;
import com.stone.framework.message.task.JobRegisterResBody;
import com.stone.framework.message.task.JobStopReqBody;
import com.stone.framework.task.service.JobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "job")
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @ApiOperation(value = "获取job列表", httpMethod = "POST")
    @PostMapping(value = "/getNames")
    ResMessage<List<JobRegisterResBody>> getNames(@Valid @RequestBody ReqMessage<Void> reqMessage) {
        List<JobRegisterResBody> list = jobService.getNames();
        return ResMessage.success(list);
    }

    @ApiOperation(value = "启动job", httpMethod = "POST")
    @PostMapping(value = "/start")
    ResMessage<Void> strart(@Valid @RequestBody ReqMessage<JobLanuchReqBody> reqMessage) {
        jobService.start(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "停止job", httpMethod = "POST")
    @PostMapping(value = "/stop")
    ResMessage<Void> stop(@Valid @RequestBody ReqMessage<JobStopReqBody> reqMessage) {
        jobService.stop(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "重启job", httpMethod = "POST")
    @PostMapping(value = "/restart")
    ResMessage<Void> restart(@Valid @RequestBody ReqMessage<JobStopReqBody> reqMessage) {
        jobService.restart(reqMessage.getBody());
        return ResMessage.success();
    }

}
